package com.ht.shop.controller;

import com.google.gson.Gson;
import com.ht.shop.annotations.RequestMapping;

import com.ht.shop.pojo.TbGoodsType;
import com.ht.shop.service.GoodsTypeService;
import com.ht.shop.service.impl.GoodsTypeServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.SQLException;
import java.util.List;

//@WebServlet("/goodtype")
@RequestMapping("/goodtypes")
public class GoodsTypeServlet  {
    private Gson gson = new Gson();
    private GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       doPost(req,resp);
//    }
//
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String method = req.getParameter("method");
//        switch (method) {
//            case "findallsecond":
//                findallsecond(req,resp);
//                break;
//        }
//    }

//
    @RequestMapping("/add")
    public String add(HttpServletRequest request,HttpServletResponse response) {

        try {
//            System.out.println(request.getParameter("parent"));
            goodsTypeService.addType(request.getParameter("name"),request.getParameter("Parent"));
            List<TbGoodsType> goodsTypeList = (List<TbGoodsType>) request.getServletContext().getAttribute("goodsTypeList");
            List<TbGoodsType> allTypes = goodsTypeService.findAllSecondLevelType();
            goodsTypeList.clear();//清除原先的数据，一定要先清除，因为list允许重复
            goodsTypeList.addAll(allTypes);//重新添加
            return "redirect:/admin/showGoodsType.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/error/error.jsp";
        }

    }
    @RequestMapping("/delete")
    public String delete(HttpServletRequest request,HttpServletResponse response) {
        String id = request.getParameter("id");
        int i = goodsTypeService.deleteType(id);
        if (i == -1) {
            response.setContentType("text/html;charset=utf-8");
            return "不允许删除非空节点";
        }

        try {
            List<TbGoodsType> goodsTypeList = (List<TbGoodsType>) request.getServletContext().getAttribute("goodsTypeList");
            List<TbGoodsType> allType = goodsTypeService.findAllSecondLevelType();
            goodsTypeList.clear();
            goodsTypeList.addAll(allType);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "redirect:/admin/showGoodsType.jsp";
    }
    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request,HttpServletResponse response) {
        List<TbGoodsType> allTypes = (List<TbGoodsType>) request.getServletContext().getAttribute("goodsTypeList");
        if (allTypes == null||allTypes.size() == 0) {
            try {
                allTypes = goodsTypeService.findAllSecondLevelType();
                request.getServletContext().setAttribute("goodsTypeList",allTypes);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        String json = gson.toJson(allTypes);
        System.out.println(json);
        response.setContentType("application/json;charset=utf-8");
        return json;
    }
}
